import type { MenuData } from "~@/layouts/basic-layout/typing";
import dynamicRoutes, { rootRoute } from "~@/router/dynamic-routes";
import { genRoutes } from "~@/router/generate-route";
import type { User } from "@/types";

export const useUserStore = defineStore("user", () => {
  const routerData = shallowRef();
  const menuData = shallowRef<MenuData>([]);
  const userInfo = shallowRef<User | undefined>({});

  const avatar = computed(() => userInfo.value?.headerImg);
  const nickName = computed(() => userInfo.value?.nickName);

  const setUserInfo = (value: User | undefined) => {
    userInfo.value = value;
  };
  const removeUserInfo = () => (userInfo.value = {});

  const generateRoutes = async () => {
    const currentRoute = {
      ...rootRoute,
      children: dynamicRoutes,
    };
    menuData.value = genRoutes(dynamicRoutes);
    return currentRoute;
  };

  const generateDynamicRoutes = async () => {
    const routerDatas = await generateRoutes();
    routerData.value = routerDatas;
    return routerDatas;
  };

  return {
    routerData,
    menuData,
    generateDynamicRoutes,
    userInfo,
    avatar,
    nickName,
    setUserInfo,
    removeUserInfo,
  };
});
